<template>
  <ContentWrap>
    <fromTitle title="项目管理" :hasPermi="'qis:project-managemenr:query'"  :queryParams="queryParams" @search="getList" @reset="resetQuery" /> 

    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >

      <el-form-item label="项目名称" prop="name" label-width="100">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>


      <el-form-item label="甲方单位" prop="firstpartyId">
        <el-input
          v-model="queryParams.firstpartyName"
          placeholder="请输入甲方单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contractNum">
        <el-input
          v-model="queryParams.contractNum"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label-width="100" label="项目起始时间" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          placeholder="请选择项目起始时间"
          class="!w-240px"
          range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item label-width="100" label="项目截止时间" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
            class="!w-240px"
        />
      </el-form-item>



    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
      <el-button type="warning" v-hasPermi="['qis:project-managemenr:create']" @click="openForm">新增</el-button>
    <el-button type="info" v-hasPermi="['qis:project-managemenr:export']" @click="handleExport">导出</el-button>
    <el-button type="danger" v-hasPermi="['qis:project-managemenr:delete']" @click="handleDelete">删除</el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox @change="handleSelect(scope.row)" v-model="scope.row.selected" />
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="name" >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row)"
          >
            {{ scope.row.name }}
          </el-button>
        </template>

        </el-table-column>
      <!-- <el-table-column label="机构地址" align="center" prop="address" />
      <el-table-column label="法人单位名称" align="center" prop="legalUnit" /> -->
      <el-table-column label="项目起始日期" align="center" prop="startTime" />
      <el-table-column label="项目截止日期" align="center" prop="endTime" />
      <el-table-column label="合同编号" align="center" prop="contractNum" />
      <el-table-column label="承检机构" align="center" prop="orgNames" >
          <template #default="scope">
            <span>{{ scope.row.orgNames.replace(';' , ' ') }}</span>
          </template>
        </el-table-column>
      <!-- <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProjectManagemenrForm title="" ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ProjectManagemenrApi, ProjectManagemenrVO } from '@/api/qis/projectmanagemenr'
import ProjectManagemenrForm from './ProjectManagemenrForm.vue'
import { useRouter } from 'vue-router'

/** 质检平台-项目管理 列表 */
defineOptions({ name: 'ProjectManagemenr' })

const router = useRouter() // 路由
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProjectManagemenrVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  name: undefined,
  startTime: [],
  endTime: [],
  contractAmount: undefined,
  firstpartyId: undefined,
  contractNum: undefined,
  projectManager: undefined,
  orgIds: undefined,
  thirdpartyId: undefined,
  firstpartyName:undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectManagemenrApi.getProjectManagemenrPage(queryParams)
    console.log(data)
    list.value = data.list
        list.value.forEach(e=>{
    e.selected  = false
    
   })
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.firstpartyName = undefined

  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}


const selectedList = ref<number[]>([])

const ids = ref<string>('')
/** 选择操作 */
const handleSelect = (row: ProjectManagemenrVO) => {
  console.log(row.selected)
  if (row.selected) {
    
    selectedList.value.push(row.id)
  } else {
    selectedList.value =   selectedList.value.filter(item=>item != row.id)
  }

  console.log(selectedList.value,'value')
  ids.value = selectedList.value.join(';')
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = () => {
  router.push('/inspection-info-management/project-management/add')
}

/** 修改按钮操作 */
const handleUpdate = (row: ProjectManagemenrVO) => {
  router.push({
    path: '/inspection-info-management/project-management/add',
    query: { id: row.id }
  })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    if(ids.value.length>0){
      ElMessageBox.confirm('确定删除选中数据吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
            // 删除的二次确认
    // await message.delConfirm()
    // 发起删除
    await ProjectManagemenrApi.deleteProjectManagemenr(ids.value)
    message.success(t('common.delSuccess'))
    ids.value = []
    // 刷新列表
    await getList()
  }).catch(() => {
  })
    }else {
      message.error('请选择要删除的数据')
    }

  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  if(total.value > 10000 ) return message.warning('当前数据过大，请筛选数据后导出 ')
  try {
if(ids.value.length>0){
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProjectManagemenrApi.exportProjectManagemenr({ids:ids.value,...queryParams})
    download.excel(data, '质检平台-项目管理.xls')
} else{
   // 导出的二次确认
   await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProjectManagemenrApi.exportProjectManagemenr(queryParams)
    download.excel(data, '质检平台-项目管理.xls')
}

  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>


<style lang="scss" scoped>

.table-btn{
  padding: 10px 50px;
}
</style>